﻿myApp.controller('AgentsController', function ($scope, $http, DUMMY_DATA, $location,$rootScope) {
    $scope.$emit('newPageLoaded', {
        'title': 'Our Real Estate Negotiator | ShiJie Property',
        'description': "ShiJie Property Real Estate Negotiators. We Work Very Closely With Developers. Over 40 Years Experience. Talk To One Of Our Real Estate Specialist Today!",
        'keywords': 'our real estate negotiator, shijie real estate negotiator'
    });

    $scope.isAndroid = $rootScope.isAndroid;

    pageInit();

    $scope.sortOption = "Sorted by Name (A to Z)";

    var BASE_URL;
    if ($location.host() == "localhost") {
        BASE_URL = $location.protocol() + "://" + "localhost:" + $location.port();
    } else {
        BASE_URL = $location.protocol() + "://" + $location.host();
    }


    $scope.getShareURL = function (p) {
        //  return (BASE_URL + "/property/" + p.propertyid + "/S" + p.Zoning[0]);
        return (BASE_URL + "/agent/" + p.staffid);
    }

    $scope.getShareTitle = function (p) {
        return p.username;
    }

    $scope.getShareDescription = function (p) {
        return "Email: " + p.email + "  Mobile: " + p.mobile;
    }

    $scope.getShareImageURL = function (p) {
        return (p.PropertyURL || "https://c1.staticflickr.com/3/2280/32796837675_cd6b733c19_m.jpg");
    }

    $scope.getShareGoogleImageURL = function (p) {
        var url = (p.PropertyURL || "https://c1.staticflickr.com/3/2280/32796837675_cd6b733c19_m.jpg");
        return encodeURI(encodeURI(url));
    }


    $scope.go = function (key, agentObject, ren) {
        // console.log("TRANSACT: " + JSON.stringify(agentObject));
        // console.log("CLICK!");
        var jsonObj = JSON.stringify(agentObject);
        sessionStorage.setItem(key, jsonObj);
        sessionStorage.setItem('ren', ren);
    }

    $scope.paginate = function (value) {
        var begin, end, index;
        begin = ($scope.currentPage - 1) * $scope.numPerPage;
        end = begin + $scope.numPerPage;
        index = $scope.searchData.indexOf(value);
        return (begin <= index && index < end);
    };

    $scope.updateFilterOption = function (option, sortedChanged) {
        $scope.SortOptionChanged = sortedChanged;
        //$scope.$apply();
        console.log("HEY!" + option);
        switch (option) {
            case 0:
                $scope.sortOption = "Sorted by Name (A to Z)";
                $scope.sortKey = 'username';
                break;
            case 1:
                $scope.sortOption = "Sorted by Name (Z to A)";
                $scope.sortKey = '-username';
                break;
        
        }
    }

    $scope.isEmpty = function (obj) {
        for (var i in obj) if (obj.hasOwnProperty(i)) return false;
        return true;
    };


    function pageInit() {
        if (DUMMY_DATA == true) {
            var dummyData = [
  {
      "staffid": "b56a6ecc-c278-4668-95d8-bfea3795848b",
      "initial": "AT",
      "username": "ALAN TAN",
      "mobile": "0178359233",
      "email": "alantankt@lskworld.com",
      "largeUrl": "https://farm9.staticflickr.com/8872/28220989626_e147aa5a86_b.jpg",
      "mediumUrl": "https://farm9.staticflickr.com/8872/28220989626_e147aa5a86.jpg",
      "Ren": "",
      "Description": null,
      "Area": null,
      "PropertyType": null
  },
  {
      "staffid": "01e38f67-39f1-435a-94a8-21847ea4afe0",
      "initial": "ALVIN",
      "username": "ALVIN TEH",
      "mobile": "0182575717",
      "email": "tjt@lskworld.com",
      "largeUrl": "https://farm1.staticflickr.com/516/19162517368_59915b9b25_b.jpg",
      "mediumUrl": "https://farm1.staticflickr.com/516/19162517368_59915b9b25.jpg",
      "Ren": "REN 08399",
      "Description": "Hello my name is Alvin Teh",
      "Area": "Butterworth",
      "PropertyType": "Land, Bungalow"
  },
  {
      "staffid": "26094339-75aa-4419-9932-346228adff55",
      "initial": "ANN",
      "username": "ANNIE OON",
      "mobile": "0124558062",
      "email": "annie@lskworld.com",
      "largeUrl": "https://farm1.staticflickr.com/390/18727724354_27ec08511c_b.jpg",
      "mediumUrl": "https://farm1.staticflickr.com/390/18727724354_27ec08511c.jpg",
      "Ren": "REN 08402",
      "Description": null,
      "Area": null,
      "PropertyType": null
  },
  {
      "staffid": "492ae819-4f18-43f6-971c-a7b339735cc8",
      "initial": "SOON",
      "username": "COON KEONG",
      "mobile": "0174236232",
      "email": "coonkeong@lskworld.com",
      "largeUrl": "https://farm2.staticflickr.com/1682/25896582973_9e3206dc8f_b.jpg",
      "mediumUrl": "https://farm2.staticflickr.com/1682/25896582973_9e3206dc8f.jpg",
      "Ren": "",
      "Description": null,
      "Area": null,
      "PropertyType": null
  },
  {
      "staffid": "b448c97d-8494-4d3e-ba07-250764357c6c",
      "initial": "DANNIS",
      "username": "DANNIS",
      "mobile": "0125364191",
      "email": "dannislim@lskworld.com",
      "largeUrl": "https://farm1.staticflickr.com/671/31624568621_4fec182987_b.jpg",
      "mediumUrl": "https://farm1.staticflickr.com/671/31624568621_4fec182987.jpg",
      "Ren": "0",
      "Description": "",
      "Area": "",
      "PropertyType": "0"
  },
  {
      "staffid": "1dc8e19f-2413-46b6-a2be-45df9afc1aa3",
      "initial": "EJOY",
      "username": "EJOY",
      "mobile": "0125658969",
      "email": "ejoy@lskworld.com",
      "largeUrl": "https://farm1.staticflickr.com/565/32119507925_22ea2d78fa_b.jpg",
      "mediumUrl": "https://farm1.staticflickr.com/565/32119507925_22ea2d78fa.jpg",
      "Ren": "0",
      "Description": "I AN NEW IN THIS INDUSTRY AND I HOPE THAT YOU CAN GIVE ME A CHANCE TO SERVE YOU",
      "Area": "PENANG ISLAND",
      "PropertyType": "AUCTION AND SUB SALES"
  },
  {
      "staffid": "1c3d41c2-a29a-44ce-8247-efd8cf67d75d",
      "initial": "CHOON",
      "username": "FOH CHOON SENG",
      "mobile": "0164402800",
      "email": "csfoh@lskworld.com",
      "largeUrl": "https://farm2.staticflickr.com/1617/26473478476_0ae66e380d_b.jpg",
      "mediumUrl": "https://farm2.staticflickr.com/1617/26473478476_0ae66e380d.jpg",
      "Ren": null,
      "Description": null,
      "Area": null,
      "PropertyType": null
  },
  {
      "staffid": "65a9016e-676b-4ee3-b79f-89288740c7a2",
      "initial": "BENG",
      "username": "FRANKIE LEE",
      "mobile": "0174417313",
      "email": "frankielee@lskworld.com",
      "largeUrl": "https://farm4.staticflickr.com/3772/19344073372_9dc6c6befe_b.jpg",
      "mediumUrl": "https://farm4.staticflickr.com/3772/19344073372_9dc6c6befe.jpg",
      "Ren": "REN 08371",
      "Description": null,
      "Area": null,
      "PropertyType": null
  },
  {
      "staffid": "85e4e1f2-2061-42da-bd10-8f25909070c7",
      "initial": "GKW",
      "username": "GOH KHAI WEI",
      "mobile": "0103828873",
      "email": "gohkhaiwei@lskworld.com",
      "largeUrl": "https://farm1.staticflickr.com/412/31489403096_cbe381b8d6_b.jpg",
      "mediumUrl": "https://farm1.staticflickr.com/412/31489403096_cbe381b8d6.jpg",
      "Ren": "0",
      "Description": null,
      "Area": null,
      "PropertyType": null
  },
  {
      "staffid": "ff84c8dd-e80f-4956-b7ba-9d6d6c663bad",
      "initial": "JT",
      "username": "JT TAN",
      "mobile": "0123926682",
      "email": "jttan@lskworld.com",
      "largeUrl": "https://farm2.staticflickr.com/1550/25894540864_d66403f314_b.jpg",
      "mediumUrl": "https://farm2.staticflickr.com/1550/25894540864_d66403f314.jpg",
      "Ren": "REN 08365",
      "Description": null,
      "Area": null,
      "PropertyType": null
  },
  {
      "staffid": "e1438e56-ddb1-4925-a202-eeb8cb5b32a5",
      "initial": "KJ",
      "username": "KATHLEEN JIM",
      "mobile": "0124033027",
      "email": "kathleenjim@lskworld.com",
      "largeUrl": "https://farm1.staticflickr.com/568/32072060046_3057bbeee7_b.jpg",
      "mediumUrl": "https://farm1.staticflickr.com/568/32072060046_3057bbeee7.jpg",
      "Ren": "0",
      "Description": "",
      "Area": "",
      "PropertyType": "0"
  },
  {
      "staffid": "3e6576a3-4e42-4623-baa2-86b20c546a33",
      "initial": "WS KHOO",
      "username": "KHOO WEI SHENG",
      "mobile": "0189654344",
      "email": "ws@lskworld.com",
      "largeUrl": "https://farm9.staticflickr.com/8139/29450596642_e867907645_b.jpg",
      "mediumUrl": "https://farm9.staticflickr.com/8139/29450596642_e867907645.jpg",
      "Ren": "0",
      "Description": null,
      "Area": null,
      "PropertyType": null
  },
  {
      "staffid": "91296169-8651-4a10-8ea5-34a1697f7d28",
      "initial": "LSK",
      "username": "LONG SOO KEAT",
      "mobile": "0183987777",
      "email": "longsookeat@lskworld.com",
      "largeUrl": "https://farm2.staticflickr.com/1459/26499442055_4e0cb4ca3c_b.jpg",
      "mediumUrl": "https://farm2.staticflickr.com/1459/26499442055_4e0cb4ca3c.jpg",
      "Ren": "PEA 1161",
      "Description": null,
      "Area": null,
      "PropertyType": null
  },
  {
      "staffid": "eefde287-d76b-418b-9577-9f30361f4ac2",
      "initial": "MAY",
      "username": "MAYZEL WONG",
      "mobile": "0175758999",
      "email": "mayzel@lskworld.com",
      "largeUrl": "https://farm4.staticflickr.com/3944/18729572383_1595f62376_b.jpg",
      "mediumUrl": "https://farm4.staticflickr.com/3944/18729572383_1595f62376.jpg",
      "Ren": "REN 17359",
      "Description": null,
      "Area": null,
      "PropertyType": null
  },
  {
      "staffid": "ab78e02c-6622-4a6b-9531-40e7e4ce6f46",
      "initial": "MIKI LIM",
      "username": "MIKI LIM CHUN SIEW",
      "mobile": "0134817777",
      "email": "enquiries@lskworld.com",
      "largeUrl": "https://farm8.staticflickr.com/7360/27380694890_80ba24fab0_b.jpg",
      "mediumUrl": "https://farm8.staticflickr.com/7360/27380694890_80ba24fab0.jpg",
      "Ren": "REN 08363",
      "Description": null,
      "Area": null,
      "PropertyType": null
  },
  {
      "staffid": "d9899bc7-f9b5-4fca-a3b3-e75021ebac77",
      "initial": "MN",
      "username": "MONY NG",
      "mobile": "0124276737",
      "email": "monyng@lskworld.com",
      "largeUrl": "https://farm4.staticflickr.com/3872/18729593823_bb79eae979_b.jpg",
      "mediumUrl": "https://farm4.staticflickr.com/3872/18729593823_bb79eae979.jpg",
      "Ren": "REN 08387",
      "Description": null,
      "Area": null,
      "PropertyType": null
  },
  {
      "staffid": "b9fe0256-e070-473f-86f0-c6bb9bfa0531",
      "initial": "OOI",
      "username": "OOI JYH SIANG",
      "mobile": "0172509595",
      "email": "jsooi@lskworld.com",
      "largeUrl": "https://farm9.staticflickr.com/8572/28152375032_5b2e150bce_b.jpg",
      "mediumUrl": "https://farm9.staticflickr.com/8572/28152375032_5b2e150bce.jpg",
      "Ren": "REN 17362",
      "Description": null,
      "Area": null,
      "PropertyType": null
  },
  {
      "staffid": "898e4c76-2654-4413-8dc2-5d8e1246c571",
      "initial": "PS",
      "username": "PATRICK SIOW",
      "mobile": "0194189918",
      "email": "patricksiow@lskworld.com",
      "largeUrl": "https://farm9.staticflickr.com/8846/28178296991_21be68b66e_b.jpg",
      "mediumUrl": "https://farm9.staticflickr.com/8846/28178296991_21be68b66e.jpg",
      "Ren": "REN 08377",
      "Description": null,
      "Area": null,
      "PropertyType": null
  },
  {
      "staffid": "92254a34-aa2a-4df5-ba58-65817b84279c",
      "initial": "RAY",
      "username": "RAYMOND",
      "mobile": "0182202821",
      "email": "raymond@lskworld.com",
      "largeUrl": "https://farm2.staticflickr.com/1457/26473641096_4bbe02a1a8_b.jpg",
      "mediumUrl": "https://farm2.staticflickr.com/1457/26473641096_4bbe02a1a8.jpg",
      "Ren": "",
      "Description": null,
      "Area": null,
      "PropertyType": null
  },
  {
      "staffid": "c3941803-c08f-45e2-a3d2-3dc83c43a3a0",
      "initial": "HAU",
      "username": "ROBERT CHU",
      "mobile": "0125993677",
      "email": "robertchu@lskworld.com",
      "largeUrl": "https://farm1.staticflickr.com/260/19162622820_67e28d1ec3_b.jpg",
      "mediumUrl": "https://farm1.staticflickr.com/260/19162622820_67e28d1ec3.jpg",
      "Ren": "REN 11576",
      "Description": null,
      "Area": null,
      "PropertyType": null
  },
  {
      "staffid": "0eb5cae9-64c6-42bb-aa70-6608c560a0ec",
      "initial": "ROMEO",
      "username": "ROMEO NG",
      "mobile": "0174237898",
      "email": "romeong@lskworld.com",
      "largeUrl": "https://farm1.staticflickr.com/781/31359936364_c7ea738fd1_b.jpg",
      "mediumUrl": "https://farm1.staticflickr.com/781/31359936364_c7ea738fd1.jpg",
      "Ren": "08400",
      "Description": "",
      "Area": "",
      "PropertyType": "0"
  },
  {
      "staffid": "4ad68dad-1092-4545-bfce-6e00348f33a5",
      "initial": "RL",
      "username": "ROY LIM",
      "mobile": "0124311122",
      "email": "roylim@lskworld.com",
      "largeUrl": "https://farm8.staticflickr.com/7393/27975253740_e9cbcfb611_b.jpg",
      "mediumUrl": "https://farm8.staticflickr.com/7393/27975253740_e9cbcfb611.jpg",
      "Ren": "REN 08368",
      "Description": null,
      "Area": null,
      "PropertyType": null
  },
  {
      "staffid": "7ac95836-8cb5-41ba-a28e-1b95de8ceab3",
      "initial": "SHANE",
      "username": "SHANE TAN",
      "mobile": "0124090210",
      "email": "shanetan@lskworld.com",
      "largeUrl": "https://farm1.staticflickr.com/268/31363464690_a52b4da72b_b.jpg",
      "mediumUrl": "https://farm1.staticflickr.com/268/31363464690_a52b4da72b.jpg",
      "Ren": "14125",
      "Description": "HI, I AM SHANE TAN.I HAVE 3 YEARS PLUS EXPERIENCE IN REAL ESTATE LINE",
      "Area": "PENANG ISLAND,GEORGETOWN",
      "PropertyType": "RESIDENTIAL AND COMMERCIAL"
  },
  {
      "staffid": "8975a43d-36c2-4699-a16a-cefd4d7cabce",
      "initial": "SOFIA",
      "username": "SOFIA ISMAIL",
      "mobile": "0133261088",
      "email": "sofia@lskworld.com",
      "largeUrl": "https://farm9.staticflickr.com/8034/28867620644_d540679c7f_b.jpg",
      "mediumUrl": "https://farm9.staticflickr.com/8034/28867620644_d540679c7f.jpg",
      "Ren": "0",
      "Description": null,
      "Area": null,
      "PropertyType": null
  },
  {
      "staffid": "783322d9-f9a4-43d7-89bf-b63812f49173",
      "initial": "SF",
      "username": "STEPHEN FOO",
      "mobile": "0124852981",
      "email": "stephenfoo@lskworld.com",
      "largeUrl": "https://farm4.staticflickr.com/3774/18727760624_ca2c205e61_b.jpg",
      "mediumUrl": "https://farm4.staticflickr.com/3774/18727760624_ca2c205e61.jpg",
      "Ren": "REN 19703",
      "Description": null,
      "Area": null,
      "PropertyType": null
  },
  {
      "staffid": "b58e6dd8-9fad-4c24-b984-d96b9c8cee2d",
      "initial": "TEH",
      "username": "TEH HUN LIM",
      "mobile": "0194494825",
      "email": "tehhunlim@lskworld.com",
      "largeUrl": "https://farm1.staticflickr.com/358/19164134319_2279636dc4_b.jpg",
      "mediumUrl": "https://farm1.staticflickr.com/358/19164134319_2279636dc4.jpg",
      "Ren": "REN 17360",
      "Description": null,
      "Area": null,
      "PropertyType": null
  },
  {
      "staffid": "0e075adf-ee02-449d-9cf1-01344121df75",
      "initial": "TKL",
      "username": "TEH KAI LOON",
      "mobile": "0124240123",
      "email": "keironteh@lskworld.com",
      "largeUrl": "https://farm8.staticflickr.com/7512/27584879561_8695784b77_b.jpg",
      "mediumUrl": "https://farm8.staticflickr.com/7512/27584879561_8695784b77.jpg",
      "Ren": "REN 11580",
      "Description": null,
      "Area": null,
      "PropertyType": null
  },
  {
      "staffid": "9a0716b4-f0f9-4721-833b-655cd21b5545",
      "initial": "TEA",
      "username": "TEH SIU SHAN",
      "mobile": "0198007007",
      "email": "ssteh@lskworld.com",
      "largeUrl": "https://farm4.staticflickr.com/3716/19624993906_70d5207bfe_b.jpg",
      "mediumUrl": "https://farm4.staticflickr.com/3716/19624993906_70d5207bfe.jpg",
      "Ren": "REN 19704",
      "Description": null,
      "Area": null,
      "PropertyType": null
  },
  {
      "staffid": "33a2a8db-c622-4ef3-8c78-56631f696da3",
      "initial": "WES",
      "username": "WESLEY TAN",
      "mobile": "0174251993",
      "email": "wesleytan@lskworld.com",
      "largeUrl": "https://farm1.staticflickr.com/756/30714516063_12e861f350_b.jpg",
      "mediumUrl": "https://farm1.staticflickr.com/756/30714516063_12e861f350.jpg",
      "Ren": "0",
      "Description": null,
      "Area": null,
      "PropertyType": null
  }
            ];
            $scope.searchData = dummyData;

            $scope.totalItems = $scope.searchData.length;
            $scope.currentPage = 1;
            $scope.numPerPage = 10;
        } else {
            var url = 'api/User/GetAgentList';
            $http.get(url).success(function (data) {
                $scope.searchData = data;
                console.log(data);

                $scope.totalItems = $scope.searchData.length;
                $scope.currentPage = 1;
                $scope.numPerPage = 10;

            }).error(function (err) {
                alert(JSON.stringify(err));
            });
        }
        

       
    }

});